package com.zx.platform.filter;

import com.zx.platform.controller.LoginController;
import com.zx.platform.service.domain.ManageSessionDomain;
import com.zx.platform.service.domain.MenusDomain;
import com.zx.platform.service.model.MenusModel;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SecurityManageFilter extends HttpServlet implements Filter {

	//不进行权限过滤
	private static List<String> filterUrlList = new ArrayList();
	static{
		//filterUrlList.add("/manage/account/inputTradePass");
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		// 获得登陆时的用户
		ManageSessionDomain userinfo = (ManageSessionDomain) req.getSession()
				.getAttribute("_LOGONUSER_");
		// 获取访问的url
		String requestUrl = req.getRequestURI();
		// add by wenjw 2014.9.19
		// 为选择菜单时的样式服务，舍弃cookie.设置一级菜单 select_menutype，二级菜单 select_menuid
		String menuType = req.getParameter("menuType");
		String menuId = req.getParameter("menuId");
		if (StringUtils.isNotBlank(menuType) && StringUtils.isNotBlank(menuId)) {
			request.setAttribute("select_menutype", menuType);
			request.setAttribute("select_menuid", menuId);
		} else {
			List<MenusModel> allMenus = LoginController.allMenus;
			for (MenusModel menusModel : allMenus) {
				String url = menusModel.getUrl();
				int postion = url.indexOf("?");
				if(postion > 0)
				{
					url = url.substring(0,postion);
				}
				//edit by wenjw 2016.9.12
				url = url.trim();
				if (requestUrl.contains(url)) {
					request.setAttribute("select_menutype",
							menusModel.getMenuType());
					request.setAttribute("select_menuid", menusModel.getMenuId());
					break;
				}
			}
		}
		// end 菜单处理
		// end 菜单处理
		//为什么要加这一行，加了不是控制不到具体的菜单了吗？比如菜单/manage/account/ou2Accountedit只能控制到manage/account了 by wenjw 
		//requestUrl = requestUrl.substring(0, requestUrl.lastIndexOf("/"));

		if (requestUrl.indexOf("login") > -1
				|| requestUrl.indexOf("noaction.jsp") > -1) {
			filterChain.doFilter(request, response);
			return;
		}
		if (requestUrl.indexOf("style") > -1
				|| requestUrl.indexOf("script") > -1
				|| requestUrl.indexOf("images") > -1) {
			filterChain.doFilter(request, response);
			return;
		}
		
		// 如果用户没有登录则跳到登录页面
		if (userinfo == null) {
			// 如果没有用户对象,则表示没有登陆。或是登陆的会话时间已过
			HttpServletResponse httpResponse = (HttpServletResponse) response;
			httpResponse.sendRedirect(req.getContextPath() + "/");
			return;
		}

		if (requestUrl.indexOf("manage") > -1) {
			//判断是否在不需要权限判断的url中
			for(String filterUrl:filterUrlList){
				if((req.getContextPath() + filterUrl)
						.contains(requestUrl)){
					filterChain.doFilter(request, response);
					return;
				}
			}
			//如果不在菜单数据中,也不需要进行权限判断
			boolean haveMenu = false;
			List<MenusModel> allMenus = LoginController.allMenus;
			for (MenusModel menusModel : allMenus) {
				String url = menusModel.getUrl();
				if ((requestUrl+"/").contains(url+"/")) {
					haveMenu = true;
					break;
				}
			}
			if(!haveMenu){
				filterChain.doFilter(request, response);
				return;
			}
			//在菜单控制中，并且没有在filterUrlList中，则进行权限判断
			for (MenusDomain mm : userinfo.getMenusDomain()) {
				for (MenusModel oo : mm.getMenus()) {
					// 此处进行权限过滤
					if ((req.getContextPath() + oo.getUrl())
							.contains(requestUrl)) {
						filterChain.doFilter(request, response);
						return;
					}
				}
			}
			// 如果没有权限则跳转到友好提示页面
			HttpServletResponse httpResponse = (HttpServletResponse) response;
			httpResponse.sendRedirect(req.getContextPath()
					+ "/noaction.jsp");
			return;
		} else {
			filterChain.doFilter(request, response);
			return;
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}
}

